﻿@{
    Layout = "~/Views/Shared/_LayoutAdminPage.cshtml";
}
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">角色名:</label>
                        <div class="layui-input-inline">
                            <input name="rolename" class="layui-input" placeholder="输入角色名" />
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">角色代码:</label>
                        <div class="layui-input-inline">
                            <input name="rolecode" class="layui-input" placeholder="输入角色代码" />
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">备&emsp;注:</label>
                        <div class="layui-input-inline">
                            <input name="remark" class="layui-input" placeholder="输入备注" />
                        </div>
                    </div>
                    <div class="layui-inline">
                        &emsp;
                        <button class="layui-btn icon-btn" lay-filter="roleTbSearch" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="roleTable" lay-filter="roleTable"></table>
        </div>
    </div>
</div>


<!-- 表格操作列 -->
<script type="text/html" id="roleTbBar">
    <a class="layui-btn layui-btn-xs" lay-event="permission">权限分配</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="roleEditDialog">
    <form id="roleEditForm" lay-filter="roleEditForm" class="layui-form model-form">
        <input name="id" value="0" type="hidden" />
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">角色名:</label>
            <div class="layui-input-block">
                <input name="rolename" placeholder="请输入角色名" class="layui-input"
                       lay-verType="tips" lay-verify="required" required />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">角色代码:</label>
            <div class="layui-input-block">
                <input name="rolecode" placeholder="请输入角色代码" class="layui-input"
                       lay-verType="tips" lay-verify="required" required />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注:</label>
            <div class="layui-input-block">
                <textarea name="remark" placeholder="请输入备注" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="roleEditSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>



<!-- 编辑权限设置弹窗 -->
<script type="text/html" id="ePermissionDialog">
    <div class="paper-info-group">
        <div class="layui-card">
            <div class="layui-card-header">{{d.rolename}}权限设置</div>
            <div class="layui-card-body" style="height: auto; min-height: 400px; overflow: auto; box-sizing: border-box; ">
                <div id="layPermissionTree"></div>
            </div>
        </div>

        <div class="layui-form-item text-right" style="text-align: center;">
            <button class="layui-btn" lay-filter="permissionEditSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
        @*<h3>本周工作总结</h3>
        <p>{{d.summary}}</p>
        <h3>下周工作计划</h3>
        <p>{{d.plan}}</p>
        <h3>备注</h3>
        <p>{{d.comments}}</p>
        <div class="paper-info-group-imgs" id="eDialogInfoImgs">
            <img src="{{d.img1}}" width="130px" height="130px">
            <img src="{{d.img2}}" width="130px" height="130px">
            <img src="{{d.img3}}" width="130px" height="130px">
        </div>*@
    </div>
</script>



<script>
//const { parseJSON } = require("jquery");

    layui.use(['layer', 'form', 'table', 'util', 'admin', 'zTree', 'tree', 'laytpl'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        var tree = layui.tree;
        var laytpl = layui.laytpl;

        /* 渲染表格 */
        var insTb = table.render({
            elem: '#roleTable',
            url: '/api/admin/Role/GetRoleList',
            page: true,
            toolbar: ['<p>',
                '<button lay-event="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>&nbsp;',
                '<button lay-event="del" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#xe640;</i>删除</button>',
                '</p>'].join(''),
            cellMinWidth: 100,
            cols: [[
                { type: 'checkbox' },
                { field: 'id', width: 60 },
                { field: 'rolename', title: '角色名', sort: true },
                { field: 'rolecode', title: '角色代码', sort: true },
                { field: 'remark', title: '备注', sort: true },
                {
                    field: 'createtime', title: '创建时间', templet: function (d) {
                        return util.toDateString(d.createtime);
                    }
                },
                {
                    field: 'updatetime', title: '修改时间', templet: function (d) {
                        return util.toDateString(d.updatetime);
                    }
                },
                { title: '操作', toolbar: '#roleTbBar', align: 'center', width: 200, minWidth: 200 }
            ]]
        });

        /* 表格搜索 */
        form.on('submit(roleTbSearch)', function (data) {
            insTb.reload({ where: data.field, page: { curr: 1 } });
            return false;
        });

        /* 表格工具条点击事件 */
        table.on('tool(roleTable)', function (obj) {
            if (obj.event === 'edit') { // 修改
                showEditModel(obj.data);
            } else if (obj.event === 'del') { // 删除
                doDel(obj);
            } else if (obj.event === 'permission') { // 权限
                showPermission(obj.data);
            }
        });

        // 获取选中节点的id
        function getChecked_list(data) {
            var id = "";
            $.each(data, function (index, item) {
                if (id != "") {
                    id = id + "," + item.id;
                }
                else {
                    id = item.id;
                }
                var i = getChecked_list(item.children);
                if (i != "") {
                    id = id + "," + i;
                }
            });
            return id;
        }

        // 编辑权限设置
        function showPermission(data) {
            laytpl(ePermissionDialog.innerHTML).render(data, function (html) {
                admin.open({
                    type: 1,
                    title: '编辑角色权限',
                    area: ['550px','650px'],
                    content: html,
                    success: function () {
                        var loadIndex = layer.load(2);
                        $.get('/api/admin/Role/GetPermissionList?id=' + data.id, function (res) {
                            layer.close(loadIndex);
                            res = res.replace(/ischecked/g, "checked");
                            res = JSON.parse(res);
                            
                            tree.render({
                                elem: '#layPermissionTree',
                                data: res,
                                showCheckbox: true,
                                id: 'layPermissionTree'
                            });

                            // 表单提交事件
                            form.on('submit(permissionEditSubmit)', function (res) {
                                var loadIndex = layer.load(2);
                                var checkData = tree.getChecked('layPermissionTree');
                                var list = new Array();
                                list = getChecked_list(checkData);
                                //console.log({ id: data.id, data: list });
                                $.post('/api/admin/Role/EditPermissionInfo', { id: data.id, data: list }, function (res) {
                                    layer.close(loadIndex);
                                    if (1 == res.code) {
                                        //layer.close(dIndex);
                                        layer.msg(res.message, { icon: 1 });
                                        //insTb.reload({ page: { curr: 1 } });
                                    } else {
                                        layer.msg(res.message, { icon: 2 });
                                    }
                                }, 'json');

                                return false;
                            });
                        });


                      
                    }
                });
            });
        }


        /* 表格头工具栏点击事件 */
        table.on('toolbar(roleTable)', function (obj) {
            if (obj.event === 'add') { // 添加
                showEditModel();
            } else if (obj.event === 'del') { // 删除
                var checkRows = table.checkStatus('roleTable');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要删除的数据', { icon: 2 });
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.id;
                });
                doDel({ ids: ids });
            }
        });

        /* 显示表单弹窗 */
        function showEditModel(mData) {
            admin.open({
                type: 1,
                title: (mData ? '修改' : '添加') + '角色',
                content: $('#roleEditDialog').html(),
                success: function (layero, dIndex) {
                    // 回显表单数据
                    form.val('roleEditForm', mData);
                    // 表单提交事件
                    form.on('submit(roleEditSubmit)', function (data) {
                        var loadIndex = layer.load(2);

                        $.post(mData ? '/api/admin/Role/EditRoleInfo' : '/api/admin/Role/AddRoleInfo', data.field, function (res) {
                            layer.close(loadIndex);
                            if (1 == res.code) {
                                layer.close(dIndex);
                                layer.msg(res.message, { icon: 1 });
                                insTb.reload({ page: { curr: 1 } });
                            } else {
                                layer.msg(res.message, { icon: 2 });
                            }
                        }, 'json');

                        return false;
                    });
                }
            });
        }

        /* 删除 */
        function doDel(obj) {
            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                $.post('/api/admin/Role/DeleteRoleInfo', {
                    ids: obj.data ? obj.data.id : obj.ids.join(',')
                }, function (res) {
                    layer.close(loadIndex);
                    if (res.code == 1) {
                        layer.msg(res.message, { icon: 1 });
                        insTb.reload({ page: { curr: 1 } });
                    } else {
                        layer.msg(res.message, { icon: 2 });
                    }
                }, 'json');
            });
        }

    });
</script>